var express = require('express');
var md5 = require('./../tools/md5.js')
var AES =require('./../tools/AES.js')
var router = express.Router();

var db = require('../sqldb/index.js');
var patient = require('./../model/patient.js')

/* GET users listing. */
router.post('/getPatient', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return patient(db.sequelize).findAll({
			where:{
				pDept:AES.aesOpen(req.cookies.dName)
			}
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
})
router.post('/outpatient', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return patient(db.sequelize).update({
			pDept: req.body.newText
		},{
			where:{
				pCardNum:req.body.pCardNum
			}
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
})

router.post('/setSate', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return patient(db.sequelize).update({
			height:req.body.height,
			weight:req.body.weight,
			temperature:req.body.temperature,
			breath:req.body.breath,
			pulse:req.body.pulse,
			bloodPressure:req.body.bloodPressure,
			bloodSugar:req.body.bloodSugar
		},{
			where:{
				pCardNum:req.body.pCardNum
			}
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
})
router.post('/getfPatient', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return patient(db.sequelize).findAll({
			where:{
				pDept:AES.aesOpen(req.cookies.dName),
				pQueue:['首诊列队', '2诊列队']
			}
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
})
router.post('/getaPatient', function(req, res, next) {
	return db.sequelize.transaction(function(t){
		console.log("+++++++++++++++++++")
		return patient(db.sequelize).findAll({
			where:{
				pDept:AES.aesOpen(req.cookies.dName),
				pQueue:['分诊列队', '候诊列队','就诊列队']
			}
		},{
			transaction:t
		}).then(function(data){
			res.json({
				status:1,
				msg:'',
				result:data
			})
		}).catch(function(err){
			console.log("发生错误：" + err);
		});
	})
})
module.exports = router;
